package com.yangfan.springcloud.controller;

import com.yangfan.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping(value = "/hystrix")
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;


    //------------------------------------------------服务降级------------------------------------------------

    @GetMapping(value = "/ok/{id}")
    public String ok(@PathVariable(value = "id") Long id) {
        log.info(paymentService.ok(id) + "port:" + port);
        return paymentService.ok(id) + "port:" + port;
    }


    @GetMapping(value = "/error/{id}")
    public String error(@PathVariable(value = "id") Long id) {
        log.info("线程池" + Thread.currentThread().getName() + paymentService.error(id) + "port:" + port);
        return "线程池" + Thread.currentThread().getName() + paymentService.error(id) + "port:" + port;
    }


    //------------------------------------------------服务熔断------------------------------------------------


    /**
     * 服务熔断的方法。。  当 服务错误多次的时候 就会自动停止 访问这个 接口。 如果服务恢复了 会慢慢的恢复过来
     * @param id
     * @return
     */
    @GetMapping(value = "/circuit/{id}")
    public String circuitBreaker(@PathVariable(value = "id") Long id) {
        log.info(paymentService.paymentCircuitBreaker(id) + "port:" + port);
        return paymentService.paymentCircuitBreaker(id) + "port:" + port;
    }

}
